<?
include 'includes/specs_functions.php';
echo printGoogleAnalytics();

#---------------------------------------------------
# Connect to PADS
$DB = getDB();
#-------------------------------$Session information
$THEME = $_POST["THEME"];
#---------------------------------------------------

$specId = $_POST['specId'];


if(isset($_POST['specId'])){

	$output .= "
			<script type='text/javascript'>
				var mc = false;
				var mr = false;
				$(function(){
					$('.spec_avg').children().not(':radio').hide();
					$('.spec_avg').stars({
						disabled: true
					});
				});
				$(document).ready(function(){
					$('.detail-spec-row').click(function(e) {
						var tr = $(this).closest('tr');
						var specId = tr.attr('id');
						editSpec(specId.substring(5));
					});
					$('.more-comments').click(function(e) {
						var tr = $(this).closest('tr');
						var specId = tr.attr('name');
						
						if(!mc){
							$('#more-comments-arrow').removeClass('arrow-left').addClass('arrow-down');
							mc = true;
							loadWidgetComments(specId, 5);
						}else{
							$('#more-comments-arrow').removeClass('arrow-down').addClass('arrow-left');
							mc = false;
							$('#widget-more-comments').html('');
						}
						
					});
					$('.more-ratings').click(function(e) {
						var tr = $(this).closest('tr');
						var specId = tr.attr('name');
						
						if(!mr){
							$('#more-ratings-arrow').removeClass('arrow-left').addClass('arrow-down');
							mr = true;
							loadWidgetRatings(specId, 5);
						}else{
							$('#more-ratings-arrow').removeClass('arrow-down').addClass('arrow-left');
							mr = false;
							$('#widget-more-ratings').html('');
						}
					});
				});
				
		</script>";			
			
	$table = "(".SPEC_TBL_SPECS." as spec
			  LEFT JOIN ".TBL_CHARACTERS." as user
				ON user.".TBL_CHARACTERS_CICUID." = spec.".SPEC_TBL_SPECS_USER_ID.")";
	$col = array("spec.".SPEC_TBL_SPECS_ID." as specId"
		   ,"spec.".SPEC_TBL_SPECS_USER_ID." as userId"
		   ,"spec.".SPEC_TBL_SPECS_LAYOUT." as layout"
		   ,"spec.".SPEC_TBL_SPECS_JOB_ID." as jobId"
		   ,"spec.".SPEC_TBL_SPECS_LEVEL." as level"
		   ,"spec.".SPEC_TBL_SPECS_DESCRIPTION." as description"
		   ,"spec.".SPEC_TBL_SPECS_PUBLIC." as public"
		   ,"spec.".SPEC_TBL_SPECS_CDATE." as cdate"
		   ,"spec.".SPEC_TBL_SPECS_UDATE." as udate"
		   ,"(SELECT COUNT(".SPEC_TBL_COMMENTS_ID.") FROM ".SPEC_TBL_COMMENTS." WHERE ".SPEC_TBL_COMMENTS_SPEC_ID." =\"".$specId."\") as sumComment"
		   ,"user.".TBL_CHARACTERS_NAME." as Name"
		   ,"user.".TBL_CHARACTERS_AVATAR." as Avatar"
		   ,"user.".TBL_CHARACTERS_SERVER." as Server"
		   );
	$where = array("spec.".SPEC_TBL_SPECS_ID." = \"".$specId."\"");
	$orderBy = "spec.".SPEC_TBL_SPECS_ID;
	$direction = "ASC";
	
	$spec_result = $DB->GetData($table ,$col ,true ,$where, $orderBy, $direction);
	
	//Show($result);

	
	$num = $spec_result[0];
	if($num == 1){
	
		$rating_table = SPEC_TBL_RATINGS;
		$rating_col = array("count(".SPEC_TBL_RATINGS_ID.") as count"
				,"sum(".SPEC_TBL_RATINGS_VALUE.") as value");
		$rating_where = array(SPEC_TBL_RATINGS_SPEC_ID ."= \"".$specId."\"");
		$rating_orderBy = "count(".SPEC_TBL_RATINGS_ID.")";
		$rating_direction = "ASC";
		
		$rating_result = $DB->GetData($rating_table ,$rating_col ,true ,$rating_where, $rating_orderBy, $rating_direction);
		
		
		
		foreach ($rating_result[3] as $row) {
			$count = $row["count"];
			$value = $row["value"];
			
			if($count != 0){
				$average = floor($value / $count);
			}else{
				$count = 0;
				$average = 0;
			}
		}
		
		foreach ($spec_result[3] as $row) {
			$padCICUID = $row['userId'];
			$output .= "
					<script type=\"text/javascript\" src=\"../../scripts/tooltip/tooltip.js\"></script>	
					<table>
						<tr>
							<td align=\"left\" width=\"13%\">
								<span class=\"with-tooltip\" title='<img src=\"".$row['Avatar']."\"></img>'><img src='".$row['Avatar']."' style='width:18px;'/></span>
							</td>
							<td width=\"87%\">
								<a href='?".$row['userId']."' class='top10_name'>".$row['Name']."</a>
							</td>
						</tr>
						<tr>
							<td align=\"center\" width=\"13%\">
							</td>
							<td width=\"87%\">
								<strong style=\"font-size:10px;color: #C4C4C4;\">".$row['Server']."</strong>
							</td>
						</tr>
						<tr name='".$specId."' class='more-comments cur-point'>
							<td align=\"center\" width=\"13%\" style='border-bottom: solid 1px #F4F4F4;padding-bottom: 4px;'>
								<strong style='font-size:11px'>".$row['sumComment']."</strong>
							</td>
							<td width=\"87%\" style='border-bottom: solid 1px #F4F4F4;padding-bottom: 4px;'>
								<strong style=\"font-size:11px;\">Comments</strong>
								<div id='more-comments-arrow' class='fl-right cur-point arrow-left' style='display:inline;margin-top: 7px;'/>
							</td>
						</tr>
						<tr>
							<td id='widget-more-comments' align=\"center\" width=\"100%\" colspan='2'>
							</td>
						</tr>
						<tr name='".$specId."' class='more-ratings  cur-point'>
							<td align=\"center\" width=\"13%\" style='border-bottom: solid 1px #F4F4F4;padding-bottom: 4px;'>
								<strong style='font-size:11px'>".$count."</strong>
							</td>
							<td width=\"87%\" style='border-bottom: solid 1px #F4F4F4;padding-bottom: 4px;'>
								<strong style=\"font-size:11px;\">Votes</strong>
								<div id='more-ratings-arrow' class='fl-right cur-point arrow-left' style='display:inline;margin-top: 7px;'/>
							</td>
						</tr>
						<tr>
							<td id='widget-more-ratings' align=\"center\" width=\"100%\" colspan='2'>
							</td>
						</tr>
						<tr>
							<td align=\"left\" width=\"100%\" colspan='2'>
								<div class='fl-left'>
									<strong style=\"font-size:11px;\">Rating:</strong>
								</div>
								<div class='fl-left'>
									<div class='fl-left pad-t-10'>
										<form class='spec_avg' action='' method='post' style='padding-left: 10px;padding-top: 1px;'>";
										foreach (get_options() as $id => $title){
											$output .= 	"<input type='radio' name='rate' value='".$id."' id='avg".$id."'";
											if($id==$average){
												$output .= 	" checked";
											}
											$output .= 	" />";
										}
				$output .= 	"			</form>
									</div>
								</div>
								
							</td>
						</tr>
						<tr>
							<td align=\"left\" width=\"100%\" colspan='2'>
								<div >
									<strong style='font-size:11px;margin-right: 10px;'>Description:</strong>
								</div>
							</td>
						</tr>
						<tr>
							<td align=\"left\" width=\"100%\" colspan='2'>
								<div  style='margin-top: 5px;'>
									<strong style='font-size:10px;color: #C4C4C4;'>".$row['description']."</strong>
								</div>
							</td>
						</tr>";
				$table = SPEC_TBL_SPECS." as spec";
							$col = array(
									"spec.".SPEC_TBL_SPECS_ID." as specId"
								   ,"spec.".SPEC_TBL_SPECS_USER_ID." as userId"
								   ,"spec.".SPEC_TBL_SPECS_LAYOUT." as layout"
								   ,"spec.".SPEC_TBL_SPECS_JOB_ID." as jobId"
								   ,"spec.".SPEC_TBL_SPECS_LEVEL." as level"
								   ,"spec.".SPEC_TBL_SPECS_DESCRIPTION." as description"
								   ,"spec.".SPEC_TBL_SPECS_PUBLIC." as public"
								   ,"spec.".SPEC_TBL_SPECS_CDATE." as cdate"
								   ,"DATE_FORMAT(spec.".SPEC_TBL_SPECS_UDATE.",'%b %d %Y') as udate"
								   );
							$where = array("spec.".SPEC_TBL_SPECS_USER_ID." = \"".$padCICUID."\""
											,"spec.".SPEC_TBL_SPECS_ID." NOT IN (\"".$specId."\")"
											,"spec.".SPEC_TBL_SPECS_PUBLIC." = 'Y'");
							$orderBy = "spec.".SPEC_TBL_SPECS_ID;
							$direction = "ASC";
						
							$result = $DB->GetData($table ,$col ,true ,$where, $orderBy, $direction);		
			if($result[0] > 0){
				$output .=	"<tr>
								<td align=\"left\" width=\"100%\" colspan='2'>
									<div style='margin-top: 5px;'>
										<strong style='font-size:11px;margin-right: 10px;'>Other specs:</strong>
									</div>
								</td>
							</tr>";
			}
						
						
						
		$output .=		"<tr>
							<td align=\"left\" width=\"100%\" colspan='2'>";
							
							// OTHER SPECS 
							
				$output .= 	"	<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\" id=\"own_specs\">";



								
							
								foreach ($result[3] as $row) {
								
									$jobName = GetClass($row['jobId']);
									$output .= "
										<tr id='tr_d_".$row['specId']."' name='".$row['public']."' class='spec-row-hover cur-point detail-spec-row'>
											<td  align=\"left\" width=\"10%\" style='font-size:10px;'>
												".$row['level']."
											</td>
											<td  align=\"left\" width=\"10%\">
												<span class=\"with-tooltip\" title=\"".str_replace("\"", "'", $row['description'])."\"><img src='".GetClassIcon($jobName)."' style='width:18px;'/></span>
											</td>
											<td width=\"30%\">
												<strong style='font-size:11px'>".$jobName."</strong>
											</td>
											<td  width='35%' align='right' valign='middle' style='font-size:9px;'>
												".$row['udate']."
											</td>
										</tr>";
								}
								
							
							
							
							
							
			$output .= 	"		</table>
							</td>
						</tr>
					</table>";
		}					
	}
}

echo $output;
#------------------------------------------------------------------------
# Disconnect
leaveDB($DB);

?>
